const generator = require('@babel/generator').default //将 AST 还原成 JavaScript 代码
const parser = require("@babel/parser"); // 编译成语法树
const traverse = require("@babel/traverse"); //对语法树进行操作
const types = require("@babel/types"); //判断、验证节点的类型、构建新 AST 节点等
var fs = require("fs"); //读取文件
window = global

js = fs.readFileSync('H5guard.js', { encoding: 'utf-8' })
let ast = parser.parse(js);


ajs = fs.readFileSync('a.js', { encoding: 'utf-8' })
window.eval(ajs)

replaceB = {
    "CallExpression" (path) {
        //B函数还原
        if (path.node.callee.name == "b") {
            path.replaceInline(types.valueToNode(eval(path.toString())))
        }
    }
}

traverse.default(ast, replaceB); // 替换B 函数

const { code } = generator(ast, opts = {
    retainLines: false,
    comments: true,
    compact: "concise"
}, js);

fs.writeFile('H5guard_decode.js', code, (err) => {});